package com.tansun.easycare.rule.ruleproj.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.modules.sys.utils.UserUtils;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.ruleproj.entity.RuleAuthority;
import com.tansun.easycare.rule.ruleproj.entity.RuleProject;
import com.tansun.easycare.rule.ruleproj.service.RuleProjectService;
import com.tansun.easycare.rule.utils.JsonUtil;

/**
 * 跳转规则模型管理action
 *
 * @author Lph
 */
@Controller
@RequestMapping(value = "/ruleproj/smartwizard")
public class SmartWizardController extends RuleBaseController {

    @Autowired
    private RuleProjectService ruleProjectService;
    @Autowired
    private RuleManageService ruleManageService;
    
    @RequestMapping("index")
    public String index(HttpServletRequest request, HttpServletResponse response, String projectCode, RuleManageVO param, ModelMap map) throws Exception {
        param.setProjectCode(this.getCurrProjectCode());
        List<RuleProject> ruleList = ruleProjectService.getUserRuleAuthProj(projectCode, null);
        if (ruleList.size() >= 1) {
            //直接进入规则项目页面 呈现所有的第一个规则项目的内容
            if (projectCode == null) {
                projectCode = ruleList.get(0).getProjectcode();
            }
            projectCode = new String(projectCode.getBytes("ISO8859-1"), "UTF-8");
            super.setCurrProjectCode(projectCode);
            map.put("projectCode", projectCode);
            map.put("ruleList", ruleList);
            PageInfo<RuleManageVO> page = this.ruleManageService.findRuleManageVO(param.getPage(), param);
            map.put("page", page);
            map.put("vo", JsonUtil.object2json(param));
            return "rule/smartwizard/index";
        } else {
            //提示没有授权的项目
            return "rule/ruleproj/noruleProj";
        }
    }
    @RequestMapping("ruleguide")
    public String smartwizard(String projectCode,ModelMap map) throws Exception {
        List<RuleProject> ruleList = ruleProjectService.getUserRuleAuthProj(projectCode, null);
        if (ruleList==null || ruleList.size() == 0) 
        	return "rule/ruleproj/noruleProj";
        List<RuleAuthority> list = ruleProjectService.getProjAuthInfo(projectCode, UserUtils.getSysService().getCurrUser().getLoginName());
        RuleAuthority currAuth = list.get(0);
        map.put("currAuth", currAuth.getAuthoritycode());
        map.put("projectCode", projectCode);
        return "rule/smartwizard/ruleguide";
    }
    @RequestMapping("rulecomponent")
    public String rulecomponent(String projectCode,ModelMap map) throws Exception {
    	map.put("projectCode", projectCode);
        return "rule/smartwizard/rulecomponent";
    }
    @RequestMapping("ruletestcomponent")
    public String ruletestcomponent(String projectCode,ModelMap map) throws Exception {
    	map.put("projectCode", projectCode);
        return "rule/smartwizard/ruletestcomponent";
    }
    @RequestMapping("noruleProj")
    public String noruleProj(String projectCode,ModelMap map) throws Exception {
    	map.put("projectCode", projectCode);
        return "rule/ruleproj/noruleProj";
    }
}
